// index.js
// 获取应用实例
const app = getApp()
import {
    Wxlogin,
    HomeList,
    getWxIndex,
    getReservationMuseum
} from "../../api/home.js";
Page({
    data: {
        text:"",
        top: null,
        dizhi: "",
        imgUrls:[],
    },
    // 事件处理函数
    //跳转重庆科技馆简介
    jianjie() {
        wx.navigateTo({
            url: '../introduction/introduction',
        })
    },
    distel() {
        var _this = this
        wx.showModal({
            title: '是否要拨打',
            content: wx.getStorageSync("tel"),
            success(res) {
                console.log(res);
                if (res.confirm) {
                    //联系我们 打电话
                    wx.makePhoneCall({
                        phoneNumber: wx.getStorageSync("tel"),
                        success: (result) => {},
                        fail: () => {},
                        complete: () => {}
                    })

                } else if (res.cancel) {
                    // console.log('用户点击取消')
                }
            }
        })
    },
      // 点击地址
      dizhi() {
        let plugin = requirePlugin('routePlan');
        let key = 'IEKBZ-XWJCV-PLHPB-UMKYX-CNOV3-DFF6K'; //使用在腾讯位置服务申请的key
        let referer = '重庆科技馆'; //调用插件的app的名称
        let themeColor = '#377FF6 ' // 插件主题色
        let navigation = 1 // 值为1时，开启驾车导航功能；默认不开启此功能
        let endPoint = JSON.stringify({ //终点
            'name': '重庆科技馆',
            'latitude': 29.570316,
            // 'latitude': wx.getStorageSync("lat"),
            // 'longitude': wx.getStorageSync("lng")
            'longitude': 106.577111
        });
        console.log(wx.getStorageSync("lat"));
        console.log(wx.getStorageSync("lng"));
        wx.navigateTo({
            url: 'plugin://routePlan/index?key=' + key + '&referer=' + referer + '&endPoint=' + endPoint + '&navigation=' + navigation + '&themeColor=' + themeColor
        });
    },
    //跳转团队预约
    tuandui(){
        wx.navigateTo({
          url: '/pages/tuanduioder/tuanduioder',
        })
    },
    //跳转个人中心
    gomine(){
        wx.navigateTo({
          url: '../mine/mine',
        })
    },
    onLoad() {
       
        //这一块是刚才那个弹出框然后获取用户的xinx
    function GetDistance(lat1, lng1, lat2, lng2) {
        var radLat1 = lat1 * Math.PI / 180.0;
        var radLat2 = lat2 * Math.PI / 180.0;
        var a = radLat1 - radLat2;
        var b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
        var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
          Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
        s = s * 6378.137; // EARTH_RADIUS;
        s = Math.round(s * 10000) / 10000;
        // console.log(lat1, lng1, lat2, lng2);
        // console.log(s/2);
        return location = s;
      }
  
      // 实时获取用户的经纬度
      let _this = this
      wx.getLocation({
        type: 'wgs84',
        success(res) {
          const latitude = res.latitude
          const longitude = res.longitude
          const speed = res.speed
          const accuracy = res.accuracy
          console.log(res);
          wx.setStorageSync("location", res.latitude)
          wx.setStorageSync("location1", res.longitude)
          console.log(location)
          // 调用 return的距离单位为km
          let distance = GetDistance(30.251243, 120.143778, wx.getStorageSync("location"), wx.getStorageSync("location1"))
          //修改用户的实时位置
          _this.setData({
            location: distance.toFixed(),
            yonghu: true
          })
          wx.setStorageSync('distance', location)

        },
        fail(error) {
          console.log(error);
          _this.setData({
            yonghu: false
          })
        }
      })
        //获取用户手机的顶部
        let system;
        const menu = wx.getMenuButtonBoundingClientRect();
        wx.getSystemInfo({
            success(res) {
                system = res
            }
        });
        const top = system.statusBarHeight + menu.height + (menu.top - system.statusBarHeight) * 2;
        this.setData({
            top: top
        })
        //用户静默登录
        var that = this
        wx.showLoading({
          title: '加载中',
        })
        wx.login({
            success(res) {
                console.log(res);
                wx.setStorageSync("code", res.code)
                if (res.code) {
                    Wxlogin({
                        code: res.code,
                    }).then((res) => {
                        console.log(res);
                        wx.setStorageSync("token", res.data.sessionId)
                        //获取首页封面图等等
                        
                        HomeList().then((res) => {
                          console.log(res);
                        that.setData({
                            text:res.data.bulletin,
                            dizhi:res.data.address
                        })
                          wx.setStorageSync('tel',res.data.contactNumber)
                          wx.setStorageSync("lat",res.data.lat)
                          wx.setStorageSync("lng",res.data.lng)
                      })

                      //获取首页baner图
                      getWxIndex().then((res)=>{
                        wx.hideLoading({})
                          console.log(res);
                          const arr = [];
                          console.log(res.data.bannerVos.sort());
                          that.setData({
                            imgUrls: res.data.bannerVos.sort()
                          })
                      })
                      getReservationMuseum().then((res)=>{
                          console.log(res);
                          if(res.code == 1000){
                              wx.setStorageSync('temporaryClosureSwitch', res.data.temporaryClosureSwitch)
                          }else{
                              wx.showModal({
                                title: res.data.msg,
                              })
                          }
                      })
                    })
                }
            }
        })
          
    },
    Jumpyy() {
        wx.navigateTo({
            url: '../individual/individual',
        })
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
		return {
		  title: '重庆科技馆',
		  path: 'pages/index/index', // 显示的页面
		
		  success: function (res) {
			// 转发成功
		  },
		  fail: function (res) {
			// 转发失败
		  }
		}
	// }
	}

})